package com.easwift.qsp.das.dvw16;
import java.io.BufferedInputStream;

import com.easwift.qsp.enums.DeviceTypeEnum;
import com.easwift.qsp.enums.MonitorItemEnum;
import com.easwift.qsp.util.DataSourceUtils;
import com.easwift.qsp.util.LogUtils;
/**
 * dvw16读数据线程
 * @author Administrator
 *
 */
public class DVWSocketReadThread extends Thread{
	
	private BufferedInputStream bis;
	//以下dvw16设备参数
	//预设参数
	private static final double K = 1;
	//位初始变量
	private static final double F0 = 1;
	
	private static final double R = 3000;
	
	private static final double B = 3950;
	
	private static final double T1 = 298.15;
	
	public DVWSocketReadThread(BufferedInputStream bis) {
		super();
		this.bis = bis;
	}
	
	@Override
	public void run(){
		try{
			while(true){
				if(bis.available() != 0){
					byte[] bytes = new byte[bis.available()];
					bis.read(bytes,0,bis.available());
					System.out.println("读取到数据：");
					LogUtils.printFrame(bytes);
					//数据长度
					int dataLength = Byte.toUnsignedInt(bytes[10]) + (Byte.toUnsignedInt(bytes[11]) << 8);
					//设备地址
					int address = Byte.toUnsignedInt(bytes[0]) + (Byte.toUnsignedInt(bytes[1]) << 8);
					//频率
					int hz = 0;
					//电阻
					int Ω = 0;
					//应变
					double f = 0;
					//温度
					double oc = 0;
					for(int i = 14;i < dataLength + 14;i = i + 8){
						//四个字节不是都为0
						if((bytes[i] | bytes[i+1] | bytes[i+2] | bytes[i+3]) != 0){
							hz = Byte.toUnsignedInt(bytes[i]) + (Byte.toUnsignedInt(bytes[i+1]) << 8) + (Byte.toUnsignedInt(bytes[i+2]) << 16) + (Byte.toUnsignedInt(bytes[i+3]) << 24);
							Ω = Byte.toUnsignedInt(bytes[i+4]) + (Byte.toUnsignedInt(bytes[i+5]) << 8) + (Byte.toUnsignedInt(bytes[i+6]) << 16) + (Byte.toUnsignedInt(bytes[i+7]) << 24);
							f = K * ((hz / 10D) * (hz / 10D) - F0 * F0) / 1000;
							oc = 1.0 / ((Math.log(Ω / R) / B) + 1.0 / T1) - 273.15;
							System.out.println(String.format("频率：%s",hz));
							System.out.println(String.format("电阻：%s",Ω));
							System.out.println(String.format("应变：%s",f));
							System.out.println(String.format("温度：%s",oc));
							String sql = String.format("insert into tb_device_data (address,value,device_type,monitor_item_id,create_time) values (%s,%s,%s,%s,%s)",String.valueOf(address),hz,DeviceTypeEnum.DVW16.getCode(),MonitorItemEnum.FREQUENCY.getCode(),"now()");
							DataSourceUtils.getJdbcTemplate().execute(sql);
							sql = String.format("insert into tb_device_data (address,value,device_type,monitor_item_id,create_time) values (%s,%s,%s,%s,%s)",String.valueOf(address),Ω,DeviceTypeEnum.DVW16.getCode(),MonitorItemEnum.OM.getCode(),"now()");
							DataSourceUtils.getJdbcTemplate().execute(sql);
							sql = String.format("insert into tb_device_data (address,value,device_type,monitor_item_id,create_time) values (%s,%s,%s,%s,%s)",String.valueOf(address),f,DeviceTypeEnum.DVW16.getCode(),MonitorItemEnum.CHANGE.getCode(),"now()");
							DataSourceUtils.getJdbcTemplate().execute(sql);
							sql = String.format("insert into tb_device_data (address,value,device_type,monitor_item_id,create_time) values (%s,%s,%s,%s,%s)",String.valueOf(address),oc,DeviceTypeEnum.DVW16.getCode(),MonitorItemEnum.TEMPERATURE.getCode(),"now()");
							DataSourceUtils.getJdbcTemplate().execute(sql);
						}
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
}